package com.glq.book.service;

import com.glq.book.dao.UserDao;
import com.glq.book.entity.User;
import com.glq.book.util.MD5Utils;
import com.glq.book.vo.LoginVO;

import java.util.List;

public class UserService {

    private UserDao userDao = new UserDao();

    /**
     * 获取所有用户
     *
     * @return
     */
    public List<User> selectUserList() {
        return userDao.selectUserList();
    }

    /**
     * 通过用户名查找用户信息
     *
     * @param username
     * @return
     */
    public User selectUserByUserName(String username) {
        if (username == null) {
            return null;
        }
        return userDao.selectUserByUserName(username);
    }

    /**
     * 登陆
     *
     * @param username
     * @param password
     * @return
     */
    public User login(String username, String password) {
        password = MD5Utils.stringToMD5(password);
        LoginVO loginVO = new LoginVO(username, password);
        return userDao.login(loginVO);
    }

    /**
     * 注册
     *
     * @param username
     * @param password
     * @return
     */
    public boolean register(String username, String password) {
        if (username == "" || username == null) {
            return false;
        }
        if (password == "" || password == null) {
            return false;
        }
        password = MD5Utils.stringToMD5(password);
        LoginVO loginVO = new LoginVO(username, password);
        return !userDao.register(loginVO);
    }

    /**
     * 更新用户信息
     *
     * @param username
     * @param trueName
     * @param age
     * @param tel
     * @return
     */
    public boolean updateUserInfoByUsername(String username, String trueName, Integer age, String tel) {
        User user = new User();
        user.setUsername(username);
        user.setTrueName(trueName);
        user.setAge(age);
        user.setTel(tel);
        return !userDao.updateUserInfoByUsername(user);
    }

    /**
     * 更新用户密码
     *
     * @param username
     * @param password
     * @return
     */
    public boolean updatePasswordByUsername(String username, String password) {
        LoginVO loginVO = new LoginVO(username, password);
        return !userDao.updatePasswordByUsername(loginVO);
    }
}
